package com.neo4j;

import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.StatementResult;

public class TestDriver {
	
	public static BaseDriver driver = new BaseDriver("bolt://127.0.0.1:7687", "neo4j", "neo4j123");
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		//bolt://127.0.0.1:7687 为API端口
		
		//String cql = "CREATE (j:JAVA { id:'java02',title:'Java入门到精通',auth:'Hyderabad',price:55})";
		//driver.runReadCQL(cql);
		//StatementResult sr = driver.runReadCQL("match (n:JAVA) return n.id,n.title,n.auth,n.price");
		
//		while (sr.hasNext()) {
//			Record r = sr.next();
//			System.out.println(r.keys());
//			System.out.println("id:"+r.get("n.id")+" title:"+r.get("n.title")+" auth:"+r.get("n.auth")+" price:"+r.get("n.price"));
//		}
		
		
//		deletePopPerson();
//		deleteChildren();
//		impPopPerson();
		//deleteChildren();
		impPopChildren();
		System.out.println("执行完毕...");
	}
	
	public static void impTest(){
		String cql = "LOAD CSV WITH HEADERS FROM \"file:///t_special_project.csv\" AS row\r\n" 
				+ "CREATE (n:T_SPECIAL_PROJECT)\r\n"
				+ "SET n = row";
		long start = System.currentTimeMillis();
		driver.runWriteCQL(cql);
		System.out.println("导入耗时："+ (System.currentTimeMillis() - start));
	}
	/**
	 * 设置父子关系
	 */
	public static void setPaternity(){
		//话person 之间的关系  测试通过
//		match (c:POP_CHILDREN),(p:POP_PERSON),(cp:POP_PERSON) where c.BIOLOGICAL_FATHER_GUID = p.GUID 
//				and c.IDENTITY_NUMBER = cp.IDENTITY_NUMBER create (p)-[cl:CP_F_LIKES]->(cp)
		
		String cql = "match (p:POP_PERSON),(c:POP_CHILDREN) where p.GUID = c.BIOLOGICAL_FATHER_GUID "
				+ "or p.GUID = c.BIOLOGICAL_MOTHER_GUID create (p)-[f:F_LIKES]->(c) return f";
		driver.runWriteCQL(cql);
	}
	public static void setChildren(){
		String cql = "match (a:POP_CHILDREN),(b:POP_PERSON),(c:POP_CHILDREN) where a.IDENTITY_NUMBER = b.IDENTITY_NUMBER "
				+ "and (c.BIOLOGICAL_FATHER_GUID = b.GUID or c.BIOLOGICAL_MOTHER_GUID = b.GUID) create (c)-[ch:C_LIKES]->(a)";
	}
	/**
	 * 导入人口数据
	 */
	public static void impPopPerson(){
		String cql = "LOAD CSV WITH HEADERS FROM \"file:///pop_person.csv\" AS row\r\n" 
				+ "CREATE (n:POP_PERSON)\r\n"
				+ "SET n = row";
		driver.runWriteCQL(cql);
	}
	/**
	 * 删除人口数据
	 */
	public static void deletePopPerson(){
		String cql = "match (p:POP_PERSON) delete p";
		driver.runWriteCQL(cql);
	}
	/**
	 * 导入孩子数据
	 */
	public static void impPopChildren(){
		long st = System.currentTimeMillis();
		for(int j = 0 ; j< 10 ; j++){
			long start = System.currentTimeMillis();
			for(int i=1;i<=10;i++){
				BaseDriver dr = new BaseDriver("bolt://127.0.0.1:7687", "neo4j", "neo4j123");
				//file 路径为neo4j数据库跟目录下的import目录下（D:\neo4j2\import）
				String cql = "USING PERIODIC COMMIT 2000 LOAD CSV WITH HEADERS FROM \"file:///sjzx_voucher."+i+".csv\" AS row\r\n" 
						+ "CREATE (n:SJZX_VOUCHER)\r\n"
						+ "SET n = row";
				dr.runWriteCQLWithoutTransation(cql);
				dr = null;
			}
			System.out.println("第"+(j+1)+"次,耗时："+ (System.currentTimeMillis() - start));
		}
		System.out.println("总耗时："+ (System.currentTimeMillis() - st));
	}
	/**
	 * 删除孩子数据
	 */
	public static void deleteChildren(){
		String cql = "match (n:POP_CHILDREN) delete n";
		driver.runWriteCQL(cql);
	}
}
